1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package scouter.javassist.bytecode;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.DataInputStream;
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import scouter.javassist.bytecode.AttributeInfo;
26 import scouter.javassist.bytecode.ConstPool;
27 import scouter.javassist.bytecode.AnnotationsAttribute.Copier;
28 import scouter.javassist.bytecode.AnnotationsAttribute.Parser;
29 import scouter.javassist.bytecode.AnnotationsAttribute.Renamer;
30 import scouter.javassist.bytecode.annotation.Annotation;
31 import scouter.javassist.bytecode.annotation.AnnotationsWriter;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class ParameterAnnotationsAttribute extends AttributeInfo {
48
49
50
51
52 public static final String visibleTag
53 = "RuntimeVisibleParameterAnnotations";
54
55
56
57
58
59 public static final String invisibleTag
60 = "RuntimeInvisibleParameterAnnotations";
61
62
63
64
65
66
67
68
69
70
71
72 public ParameterAnnotationsAttribute(ConstPool cp, String attrname,
73 byte[] info) {
74 super(cp, attrname, info);
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88 public ParameterAnnotationsAttribute(ConstPool cp, String attrname) {
89 this(cp, attrname, new byte[] { 0 });
90 }
91
92
93
94
95 ParameterAnnotationsAttribute(ConstPool cp, int n, DataInputStream in)
96 throws IOException
97 {
98 super(cp, n, in);
99 }
100
101
102
103
104 public int numParameters() {
105 return info[0] & 0xff;
106 }
107
108
109
110
111 public AttributeInfo copy(ConstPool newCp, Map classnames) {
112 Copier copier = new Copier(info, constPool, newCp, classnames);
113 try {
114 copier.parameters();
115 return new ParameterAnnotationsAttribute(newCp, getName(),
116 copier.close());
117 }
118 catch (Exception e) {
119 throw new RuntimeException(e.toString());
120 }
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135 public Annotation[][] getAnnotations() {
136 try {
137 return new Parser(info, constPool).parseParameters();
138 }
139 catch (Exception e) {
140 throw new RuntimeException(e.toString());
141 }
142 }
143
144
145
146
147
148
149
150
151
152
153 public void setAnnotations(Annotation[][] params) {
154 ByteArrayOutputStream output = new ByteArrayOutputStream();
155 AnnotationsWriter writer = new AnnotationsWriter(output, constPool);
156 try {
157 int n = params.length;
158 writer.numParameters(n);
159 for (int i = 0; i < n; ++i) {
160 Annotation[] anno = params[i];
161 writer.numAnnotations(anno.length);
162 for (int j = 0; j < anno.length; ++j)
163 anno[j].write(writer);
164 }
165
166 writer.close();
167 }
168 catch (IOException e) {
169 throw new RuntimeException(e);
170 }
171
172 set(output.toByteArray());
173 }
174
175
176
177
178
179 void renameClass(String oldname, String newname) {
180 HashMap map = new HashMap();
181 map.put(oldname, newname);
182 renameClass(map);
183 }
184
185 void renameClass(Map classnames) {
186 Renamer renamer = new Renamer(info, getConstPool(), classnames);
187 try {
188 renamer.parameters();
189 } catch (Exception e) {
190 throw new RuntimeException(e);
191 }
192 }
193
194 void getRefClasses(Map classnames) { renameClass(classnames); }
195
196
197
198
199 public String toString() {
200 Annotation[][] aa = getAnnotations();
201 StringBuilder sbuf = new StringBuilder();
202 int k = 0;
203 while (k < aa.length) {
204 Annotation[] a = aa[k++];
205 int i = 0;
206 while (i < a.length) {
207 sbuf.append(a[i++].toString());
208 if (i != a.length)
209 sbuf.append(" ");
210 }
211
212 if (k != aa.length)
213 sbuf.append(", ");
214 }
215
216 return sbuf.toString();
217
218 }
219 }